home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / IBPalettes / WW3DKit / RIBPatchMesh.m < prev    next >
Encoding:
Text File  |  1995-03-22  |  7.8 KB  |  289 lines

  1. // copyright 1993 Michael B. Johnson; some portions copyright 1994, MIT
  2. // see COPYRIGHT for reuse legalities
  3. //
  4.  
  5. #import "WW3DShape.h"
  6. #import "RIBPatchMesh.h"
  7.  
  8. @implementation RIBPatchMesh
  9.  
  10. + initialize { return [RIBPatchMesh setVersion:1], self; }
  11.  
  12. - (BOOL)hasBoundingBox { return YES; }
  13.  
  14. - init
  15. {
  16.   [super init];
  17.  
  18.   type = NULL;
  19.   return self;
  20. }
  21.  
  22.  
  23. - copyFromZone:(NXZone *)zone
  24. {
  25.    id   newCopy = [super copyFromZone:zone];
  26.  
  27.   NXLogError("WARNING: copyFromZone: incompletely implemented for %s : I'm leaking memory in -free", [[self class] name]);
  28.  
  29.   return newCopy;
  30. }
  31.  
  32.  
  33. - setType:(RtToken)newType nU:(RtInt)newNU uWrap:(RtToken)newUWrap nV:(RtInt)newNV vWrap:(RtToken)newVWrap 
  34.      n:(int)newN tokens:(RtToken *)newTokens parms:(RtPointer *)newParms archiveVector:(char **)newArchiveVector
  35.      printfTypeVector:(int *)newPrintfTypeVector printfNVector:(int *)newPrintfNVector
  36. {  
  37.    type = NXCopyStringBuffer(newType);
  38.    nU = newNU;
  39.    uWrap = NXCopyStringBuffer(newUWrap);
  40.    nV = newNV;
  41.    vWrap = NXCopyStringBuffer(newVWrap);
  42.   
  43.    [self setN:newN tokens:newTokens parms:newParms archiveVector:newArchiveVector printfTypeVector:newPrintfTypeVector printfNVector:newPrintfNVector];
  44.  
  45.    dirtyBoundingBox = TRUE;
  46.   
  47.    return self;
  48. }
  49.  
  50. - setType:(RtToken)newType  {  if (type) { free(type); }  type = NXCopyStringBuffer(newType); return self; } 
  51. - (RtToken)type { return type; }
  52. - setNU:(RtInt)newNU  {  nU = newNU; return self; } 
  53. - (RtInt)nU { return nU; }
  54. - setUWrap:(RtToken)newUWrap  {  if (uWrap) { free(uWrap); }  uWrap = NXCopyStringBuffer(newUWrap); return self; } 
  55. - (RtToken)uWrap { return uWrap; }
  56. - setNV:(RtInt)newNV  {  nV = newNV; return self; } 
  57. - (RtInt)nV { return nV; }
  58. - setVWrap:(RtToken)newVWrap  {  if (vWrap) { free(vWrap); }  vWrap = NXCopyStringBuffer(newVWrap); return self; } 
  59. - (RtToken)vWrap { return vWrap; }
  60. - (RtBasis *)uBasis  {  return &uBasis; }
  61. - (RtToken)uBasisToken  {  return uBasisToken; }
  62. - (RtInt)uStep  {  return uStep; }
  63. - (RtBasis *)vBasis  {  return &vBasis; }
  64. - (RtToken)vBasisToken  {  return vBasisToken; }
  65. - (RtInt)vStep  {  return vStep; }
  66.  
  67. // note that the code in here is largely stolen from WW3DShape's updateBases 
  68. // function.  The reason we can't use it directly is because of the need to 
  69. // start (potentially) somewhere in the middle of the list of ribCommands.
  70. - brokenGetBases 
  71. {
  72.   int   ourPosition = -1, 
  73.         howManyPositions, i;
  74.   BOOL  foundIt;
  75.   id    cmd = nil, 
  76.         ancestor = [myShape ancestor];  
  77.  
  78.  
  79.   howManyPositions = [[myShape ribCommands] count];
  80.   i = 0;
  81.   foundIt = NO;
  82.   while (!foundIt && (i < howManyPositions))
  83.   {  if (self == [[myShape ribCommands] objectAt:i])
  84.      {  ourPosition = i;
  85.         foundIt = YES;
  86.      }
  87.      else
  88.      {  i++;
  89.      }
  90.   }
  91.   if (!foundIt)
  92.   {  NXLogError("Yikes! couldn't find myself inside myShape (%d)", myShape);
  93.      return nil;
  94.   }
  95.  
  96.   i = (ourPosition - 1);
  97.   foundIt = NO;
  98.   while (!foundIt && (i > 0))
  99.   {  cmd = [[myShape ribCommands] objectAt:i];
  100.      if ([cmd respondsTo:@selector(uBasis)])
  101.      {  foundIt = YES;
  102.      }
  103.      else
  104.      {  i--;
  105.      }
  106.   }
  107.  
  108.   if (foundIt)
  109.   {  N3D_CopyMatrix(*([cmd uBasis]), uBasis);
  110.      uStep = [cmd uStep];
  111.      N3D_CopyMatrix(*([cmd vBasis]), vBasis);
  112.      vStep = [cmd vStep];
  113.   }
  114.   else
  115.   {  if (ancestor)  // get it from our ancestor
  116.      {  N3D_CopyMatrix(*([ancestor uBasis]), uBasis);
  117.         uStep = [ancestor uStep];
  118.         N3D_CopyMatrix(*([ancestor vBasis]), vBasis);
  119.         vStep = [ancestor vStep];
  120.      }
  121.      else // the shape we are in is the root; fill in bezier bases for both u and v
  122.      {  N3D_CopyMatrix(RiBezierBasis, uBasis);
  123.         uStep = 3;
  124.         N3D_CopyMatrix(RiBezierBasis, vBasis);
  125.         vStep = 3;
  126.      }
  127.   }
  128.  
  129.  
  130.   return self;
  131. }
  132.  
  133. - getBases 
  134. {
  135.   //WAVE - fix ME!!! boge for now
  136.   N3D_CopyMatrix(RiBezierBasis, uBasis);
  137.   uStep = 3;
  138.   N3D_CopyMatrix(RiBezierBasis, vBasis);
  139.   vStep = 3;
  140.  
  141.   return self;
  142. }
  143.  
  144.  
  145. - calculateBoundingBoxStartingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
  146. {
  147.   if (![self getBases])
  148.   {  return nil;
  149.   }
  150.  
  151.   // if both are bezier, just call our super's routine, since we 
  152.   // can just check against the points.  
  153.  
  154.   // ya know, we might want to have a string tag along with the bases,
  155.   // just so we don't have to deal with this epsilon bullshit...
  156.  
  157.   if (   (uBasisToken && vBasisToken) 
  158.       && (   ((!strcmp(uBasisToken, RI_BEZIER)) && (!strcmp(vBasisToken, RI_BEZIER)))
  159.           || ((!strcmp(uBasisToken, RI_BSPLINE)) && (!strcmp(vBasisToken, RI_BSPLINE)))
  160.           || ((!strcmp(uBasisToken, RI_BEZIER)) && (!strcmp(vBasisToken, RI_BSPLINE)))
  161.           || ((!strcmp(uBasisToken, RI_BSPLINE)) && (!strcmp(vBasisToken, RI_BEZIER)))))
  162.   {
  163.     [super calculateBoundingBoxStartingAt:shutterOpenTime endingAt:shutterCloseTime];
  164.     dirtyBoundingBox = FALSE; 
  165.     return self;
  166.   }
  167.  
  168.   // otherwise, need to transform one or both...
  169.   // for now, we'll still bail, and just call the super version...
  170.   [super calculateBoundingBoxStartingAt:shutterOpenTime endingAt:shutterCloseTime];
  171.   dirtyBoundingBox = FALSE; 
  172.  
  173.   return self;
  174. }
  175.  
  176.  
  177. - (BOOL)theSameAs:otherRIBCommand 
  178.   if ([self class] != [otherRIBCommand class])
  179.   {  return NO;
  180.   }
  181.   if (type != [(RIBPatchMesh *)otherRIBCommand type])  // if they're tokens, they're the same string pointer
  182.   {  if (strcmp(type, [(RIBPatchMesh *)otherRIBCommand type])) // just make sure by comparing the actual strings....
  183.      {  return NO; // okay, they really are different
  184.      }
  185.   }
  186.  
  187.   if (nU != [otherRIBCommand nU])  
  188.   {  return NO;
  189.   }
  190.   if (uWrap != [otherRIBCommand uWrap])  // if they're tokens, they're the same string pointer
  191.   {  if (strcmp(uWrap, [otherRIBCommand uWrap])) // just make sure by comparing the actual strings....
  192.      {  return NO; // okay, they really are different
  193.      }
  194.   }
  195.  
  196.   if (nV != [otherRIBCommand nV])  
  197.   {  return NO;
  198.   }
  199.   if (vWrap != [otherRIBCommand vWrap])  // if they're tokens, they're the same string pointer
  200.   {  if (strcmp(vWrap, [otherRIBCommand vWrap])) // just make sure by comparing the actual strings....
  201.      {  return NO; // okay, they really are different
  202.      }
  203.   }
  204.  
  205.   // this will check all the parameters...
  206.   return [super theSameAs:otherRIBCommand];
  207. }
  208.  
  209.  
  210. - (BOOL)similarTo:otherRIBCommand 
  211. {
  212.   if ([self class] != [otherRIBCommand class])
  213.   {  return NO;
  214.   }
  215.   if (nU != [otherRIBCommand nU])  
  216.   {  return NO;
  217.   }
  218.   if (uWrap != [otherRIBCommand uWrap])  // if they're tokens, they're the same string pointer
  219.   {  if (strcmp(uWrap, [otherRIBCommand uWrap])) // just make sure by comparing the actual strings....
  220.      {  return NO; // okay, they really are different
  221.      }
  222.   }
  223.  
  224.   if (nV != [otherRIBCommand nV])  
  225.   {  return NO;
  226.   }
  227.   if (vWrap != [otherRIBCommand vWrap])  // if they're tokens, they're the same string pointer
  228.   {  if (strcmp(vWrap, [otherRIBCommand vWrap])) // just make sure by comparing the actual strings....
  229.      {  return NO; // okay, they really are different
  230.      }
  231.   }
  232.   return YES;
  233. }
  234.  
  235.  
  236. - renderSelf:(WW3DCamera *)camera startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
  237. {
  238.   RiPatchMeshV(type, nU, uWrap, nV, vWrap, n, tokens, parms);
  239.  
  240.   return self;
  241. }
  242.  
  243. - (BOOL)isMotionBlurrable { return YES; }
  244.  
  245. - writeEve:(NXStream *)stream atTabLevel:(int)tab
  246. {
  247.    int  i;
  248.  
  249.  
  250.    for (i = 0; i < tab; i++)
  251.    {  NXPrintf(stream, "\t");
  252.    }
  253.    NXPrintf(stream, "PatchMesh %s %d %s %d %s ", type, nU, uWrap, nV, vWrap);
  254.    [super writeParameterList:stream];
  255.    return self;
  256. }
  257.  
  258. #define typeVector "*i*i*"
  259. #define typeValues &type, &nU, &uWrap, &nV, &vWrap
  260.  
  261. - read:(NXTypedStream*)stream 
  262. {
  263.     int version;
  264.     [super read:stream];
  265.  
  266.     version = NXTypedStreamClassVersion(stream,"RIBPatchMesh");
  267.     if (version == 0) NXReadTypes(stream,"i",&version), version=1;
  268.     if (version == 1)
  269.     {  NXReadTypes(stream,typeVector,typeValues);
  270.     } 
  271.     else 
  272.     {
  273.     }
  274.     return self;
  275. }
  276.  
  277. - write:(NXTypedStream*)stream 
  278. {
  279.     [super write:stream];
  280.  
  281.     NXWriteTypes(stream,typeVector, typeValues);
  282.  
  283.     return self;
  284. }
  285.  
  286.  
  287. @end
  288.